package com.zkingsoft.actions.visitor;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.dao.ProjExceptionDao;
import com.zkingsoft.dao.ProjectDao;
import com.zkingsoft.model.ProjException;
import com.zkingsoft.model.Project;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.DingDingRobotUtil;
import com.zkingsoft.plugin.util.StringUtils;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;

/**
 * 
 * @Description: 不需要登录可以访问的action
 * @author:姜友瑶
 * @date 2016年8月31日
 */
@Controller
public class ProjExceptionController extends BaseController {

	@Autowired
	ProjExceptionDao projExceptionDao;

	@Autowired
	ProjectDao projDao;

	/**
	 * 显示异常信息
	 */
	@RequestMapping(value = "/showExcptionUrl")
	public String showExcptionUrl(Long id) {
		ProjException ex = projExceptionDao.selectById(id);
		Project p = new Project();
		p.setProjNo(ex.getProjNo());
		List<Project> tempList = projDao.selectByModel(p);
		ex.setProjName(tempList.get(0).getProjName());
		WebUtil.getRequest().setAttribute("ex", ex);

		// 查询相似异常
		ProjException exQuery = new ProjException();
		exQuery.setExceptionType(ex.getExceptionType());
		List<ProjException> simeException = projExceptionDao.selectInPage(exQuery, null);
		WebUtil.getRequest().setAttribute("simeException", simeException);
		return "visitor/showException";
	}

	@RequestMapping(value = "/admin/solutionException")
	public @ResponseBody AjaxResult solutionException(ProjException ex) {
		Users user = getMe();
		ex.setDoneMan(user.getName());
		ex.setDoneTime(new Date());
		projExceptionDao.update(ex);
		return new AjaxResult(AjaxResult.STATUS_OK, null, "保存成功");
	}

	/**
	 * 显示异常信息
	 */
	@RequestMapping(value = "/admin/showException")
	public @ResponseBody AjaxResult showException(ProjException ex, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		List<ProjException> dataList = projExceptionDao.selectInPage(ex, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null, dataList,
				projExceptionDao.selectTotalRecord(ex));
		return result;
	}

	@RequestMapping(value = "/addException")
	public @ResponseBody AjaxResult addException(ProjException ex) throws IOException {

		ex.setCreateTime(new Date());
		if (StringUtils.isNotBlank(ex.getProjNo())) {
			Project p = new Project();
			p.setProjNo(ex.getProjNo());
			List<Project> tempList = projDao.selectByModel(p);
			if (tempList.size() != 1) {
				return new AjaxResult(AjaxResult.STATUS_ERR, null, "参数错误");
			}
			ex.setProjName(tempList.get(0).getProjName());
		}
		projExceptionDao.insert(ex);
		sendNoticeToAdmin(ex);

		return new AjaxResult(AjaxResult.STATUS_OK, null, "新增成功");
	}

	@Value("${is_open_dingding_exception_notice}")
	String isOpenDingdingExceptionNotice;

	@Value("${dingding_exception_token}")
	String dingdingExceptionToken;

	@Value("${showExcptionUrl}")
	String showExcptionUrl;

	private void sendNoticeToAdmin(ProjException ex) {
		if (isOpenDingdingExceptionNotice != null && "true".equals(isOpenDingdingExceptionNotice)) {
			String projectName = ex.getProjName();
			String title = projectName + "在服务器：" + ex.getMachine() + "发生异常";
			String content = "异常类型:" + ex.getExceptionType() + "  描述：" + ex.getSimpleMsg();
			DingDingRobotUtil.sendLink(dingdingExceptionToken, content, title, "",
					showExcptionUrl + "?id=" + ex.getId());
		}
	}

	@RequestMapping(value = "/admin/delException")
	public @ResponseBody AjaxResult del(String keys) {
		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = projExceptionDao.deleteByIds(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "删除失败");
		}
	}

}